package com.nodecollege.cloud.controller;

import com.nodecollege.cloud.common.UpmsConstants;
import com.nodecollege.cloud.common.annotation.ApiAnnotation;
import com.nodecollege.cloud.common.model.LoginUser;
import com.nodecollege.cloud.common.model.NCResult;
import com.nodecollege.cloud.common.model.UpmsToken;
import com.nodecollege.cloud.common.model.vo.LoginVO;
import com.nodecollege.cloud.service.LoginService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * 登陆controller
 *
 * @author LC
 * @date 2019/6/17 10:07
 */
@RestController
@RequestMapping("/user")
public class LoginController {

    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private LoginService loginService;

    /**
     * 用户注册
     */
    @ApiAnnotation(modularName = "用户登陆", description = "用户注册")
    @PostMapping("/register")
    public NCResult logon(@RequestBody LoginVO loginVO) {
        loginService.register(loginVO);
        return NCResult.ok();
    }

    /**
     * 用户登陆
     */
    @ApiAnnotation(modularName = "用户登陆", description = "用户登陆")
    @PostMapping("/login")
    public NCResult<UpmsToken> login(@RequestBody LoginVO loginVO, HttpServletResponse response) {
        // 登陆验证
        UpmsToken token = loginService.login(loginVO);
        Cookie accessTokenCookie = new Cookie(UpmsConstants.HEADER_ACCESS_TOKEN, token.getAccessToken());
        accessTokenCookie.setMaxAge(token.getExpire().intValue());
        accessTokenCookie.setPath("/");
        Cookie infoRandomCookie = new Cookie(UpmsConstants.HEADER_INFO_RANDOM, token.getInfoRandom());
        infoRandomCookie.setMaxAge(token.getExpire().intValue());
        infoRandomCookie.setPath("/");
        response.addCookie(accessTokenCookie);
        response.addCookie(infoRandomCookie);
        return NCResult.ok(Arrays.asList(token));
    }

    /**
     * 获取拥有的菜单信息
     */
    @ApiAnnotation(modularName = "用户登陆", description = "获取拥有的菜单信息")
    @PostMapping("/getUserInfo")
    public NCResult<LoginUser> getUserInfo(@RequestBody LoginVO loginVO) {
        LoginUser loginUser = loginService.getUserInfo(loginVO.getToken());
        return NCResult.ok(loginUser);
    }

    /**
     * 用户注销请求
     */
    @ApiAnnotation(modularName = "用户登陆", description = "用户注销请求")
    @PostMapping("/logout")
    public NCResult logout(@RequestBody LoginVO loginVO) {
        loginService.logout(loginVO.getToken());
        return NCResult.ok();
    }

    /**
     * 未登陆情况下重新设置密码
     *
     * @param loginVO
     * @return
     */
    @ApiAnnotation(modularName = "用户登陆", description = "未登陆情况下重新设置密码")
    @PostMapping("/updatePwdNoLogin")
    public NCResult updatePwdNoLogin(@RequestBody LoginVO loginVO) {
        loginService.updatePwdNoLogin(loginVO);
        return NCResult.ok();
    }

}
